﻿using System;
using System.Runtime.Serialization;
using System.Linq.Expressions;

namespace ExpressionSerialization
{
  [DataContract]
  public class XConditionalExpression:XExpression
  {

    // Properties 
    [DataMember]
    public XExpression Test{ get;set;}
    
    [DataMember]
    public XExpression IfTrue{ get;set;}

    [DataMember]
    public XExpression IfFalse  { get;set;}

    
    public override void FromExpression(Expression source_)
    {
      base.FromExpression(source_);
      ConditionalExpression condEx = source_ as ConditionalExpression;
      Test = XExpression.CreateFromExpression(condEx.Test);
      IfTrue = XExpression.CreateFromExpression(condEx.IfTrue);
      IfFalse = XExpression.CreateFromExpression(condEx.IfFalse);
    }

    public override Expression ToExpression(Type dataContract_, System.Collections.Generic.List<ParameterExpression> parameters_)
    {
      return Expression.Condition(Test.ToExpression(dataContract_, parameters_), IfTrue.ToExpression(dataContract_, parameters_), IfFalse.ToExpression(dataContract_, parameters_));
    }

  }
}
